package jdbc.hr.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DatabaseManager {
	
//	1.DBCP 라이브러리 추가
	private static DatabaseManager instance = null;
	
	private BasicDataSource dataSource;
	
	private DatabaseManager(){
		//BasicDataSource 객체 생성
		dataSource = new BasicDataSource();
		//DB연동을 위한 값들을 setter 메소드를 통해 대입
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
		dataSource.setUsername("scott");
		dataSource.setPassword("tiger");		
	}
	
	public static DatabaseManager getInstance(){
		if(instance ==  null){
			instance = new DatabaseManager();
		}
		return instance;
	}
	//instance 변수의 DataSource객체에서 Connection을 대여해 리턴해 주는 메소드
	public Connection getConnection() throws SQLException{
		return dataSource.getConnection();
		//여기서 연결을 못했는데 자기가 그 에러 잡아서 트라이캐취하면 뭐하겠니?
		//에러가 났다고 이놈을 호출한 얘한테 알려주고 니가 처리하라고 해야겠지?
	}
	public void close(Connection conn, PreparedStatement pstmt) throws SQLException {
		if(pstmt != null) pstmt.close();
		if(conn != null) conn.close();
		
	}
	public void close(Connection conn, PreparedStatement pstmt, ResultSet rset) throws SQLException {
		if(pstmt != null) pstmt.close();
		if(conn != null) conn.close();
		if(rset != null) rset.close();
		
	}

}
